package com.hongyuan.sellfitness.ui.find.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.google.android.material.tabs.TabLayoutMediator;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.CustomFragment;
import com.hongyuan.sellfitness.databinding.FragmentFindBinding;
import com.hongyuan.sellfitness.my_view.tablayout.MyPageTransformer;
import com.hongyuan.sellfitness.my_view.tablayout.MyTabLayoutMediator;
import com.hongyuan.sellfitness.my_view.tablayout.TabLayout;
import com.hongyuan.sellfitness.ui.Page2Adapter;
import com.hongyuan.sellfitness.ui.find.view_mode.FindViewModel;
import com.hongyuan.sellfitness.ui.find.viewpage_adapter.FindViewPagerAdapter;
import com.hongyuan.sellfitness.ui.message.fragment.MessageCommentaryFragment;
import com.hongyuan.sellfitness.ui.message.fragment.MessageLetterFragment;
import com.hongyuan.sellfitness.ui.message.fragment.MessagePraiseFragment;

import java.util.ArrayList;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class FindFragment extends CustomFragment<FragmentFindBinding, FindViewModel> {

    private List<CustomFragment> fragments;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_find;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FindViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FindViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();

        //初始化数据
        fragments = new ArrayList<>();
        fragments.add(new FindChildFragment().setTabTag("精选").setPosition("0"));
        fragments.add(new FindChildFragment().setTabTag("关注").setPosition("1"));
        fragments.add(new FindChildFragment().setTabTag("全部").setPosition("2"));

        binding.mViewPager.setAdapter(new Page2Adapter(getActivity(),fragments));
        MyTabLayoutMediator tabLayoutMediator = new MyTabLayoutMediator(binding.tab, binding.mViewPager, (tab, position) -> {
            CustomFragment f = fragments.get(position);
            tab.setText(f.getTabTag());
        });
        tabLayoutMediator.attach();

        binding.tab.post(() -> setScale(MyPageTransformer.MAX_SCALE));
        binding.mViewPager.setPageTransformer(new MyPageTransformer(binding.tab));
    }


    /**
     * 将 Tab[index] 放大为初始的 scale 倍
     */
    private void setScale(float scale) {
        LinearLayout ll = (LinearLayout) binding.tab.getChildAt(0);
        TabLayout.TabView tb = (TabLayout.TabView) ll.getChildAt(0);
        View view  = tb.getTextView();
        view.setScaleX(scale);
        view.setScaleY(scale);
    }

}
